<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules">
      <a-form-item label="奖励发放" field="reward_switch">
        <sa-radio v-model="formData.reward_switch" dict="reward-data" />
      </a-form-item>
      <a-form-item label="时长签到(分)" field="look_time">
        <a-input-number v-model="formData.look_time" placeholder="观看多久自动签到" />
      </a-form-item>
      <a-form-item label="红包发放" field="color">
        <sa-radio v-model="formData.red_switch" dict="data_status" />
      </a-form-item>
      <a-form-item label="红包金额" field="red_price">
        <a-input-number v-model="formData.red_price" placeholder="请输入红包金额" />
      </a-form-item>
      <a-form-item label="打卡次数" field="call_roll_count">
        <a-input-number v-model="formData.call_roll_count" placeholder="请输入打卡次数" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import api from '@/api/live/live'

const emit = defineEmits(['success'])

// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)

let title = computed(() => {
  return '奖励设置' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单初始值
const initialFormData = {
  room_id: '0',
  reward_switch: 1,
  look_time: 0,
  red_switch: 1,
  red_price: 0,
  call_roll_count: 0,
  config_id: '',
}

// 表单信息
const formData = reactive({ ...initialFormData })

// 验证规则
const rules = {
  look_time: [{ required: true, message: '时长签到不能为空' }],
  red_price: [{ required: true, message: '红包金额不能为空' }],
  call_roll_count: [{ required: true, message: '打卡次数不能为空' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  // 重置表单数据
  Object.assign(formData, initialFormData)
  formRef.value.clearValidate()
  visible.value = true
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  console.log(data)
  formData.config_id = data.id
  formData.room_id = data.room_id
  formData.reward_switch = data.reward_switch
  formData.look_time = Number(data.look_time)
  formData.red_switch = data.red_switch
  formData.red_price = Number(data.red_price)
  formData.call_roll_count = Number(data.call_roll_count)
}

// 设置房间id
const setroomid = (id) => {
  formData.room_id = id
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData}
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      result = await api.addReward(data)
    } else {
      // 修改数据
      result = await api.updateReward(data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData, setroomid })
</script>
